<?
	$results = array();
	require_once('../inc/Users.php');
	require_once('../inc/Keywords.php');
	if(isset($_POST['id']))
	{
		$row = $_POST;
		$results = ValidateUser($row);
		if($results === true)
			$results = SaveUser($row);
		if($results === true)
		{
			if(isset($_REQUEST['ajax'])){
				$rs = GetUser($row['id']);
				include('item.php');
			}else{
				header("Location: index.php?inserted=$row[id]#inserted");
			}
			die();
		}
	}else{
		if(isset($_REQUEST['id']))
			$row = GetUser($_REQUEST['id']);
		else
			$row = BlankUser();
	}
	$keywords = GetKeywords();//GetUserTypes();
?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Details of user: <?=$row['FirstName']?> <?=$row['LastName']?></title>
    <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css">
    <link rel="stylesheet" type="text/css" href="../inc/bootstrap.css">
    <style type="text/css" >
    	.error { color: red; }
		input.error { border-color: red; }
    </style>
</head>

<body>
        <? include('../inc/header.php'); ?>
        <div class="container">
			<? include('../inc/nav.php'); ?>
            <div id="contents">
            	
            	<dl class="error">
            		<? foreach ($results as $key => $value): ?>
						<dt><?=$key?></dt>
						<dd><?=$value?></dd>
					<? endforeach; ?>
            	</dl>
            	
                <form class="form-horizontal" method="post" action="edit.php">
                    <div class=" control-group">
                        <label class="control-label" for="FirstName">First Name:</label>
                        <div class="controls">
                            <input 	type="text" name="FirstName" id="FirstName" value="<?=$row['FirstName']?>"
									class="required  <?=isset($results['FirstName']) ? 'error' : '' ?>"
							 />
                             <span class="error"><?=isset($results['FirstName']) ? $results['FirstName'] : '' ?></span>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="LastName">Last Name:</label>
                        <div class="controls">
                            <input 	type="text" name="LastName" id="LastName" value="<?=$row['LastName']?>"
                            		class="<?=isset($results['LastName']) ? 'error' : '' ?>"
                            />
                             <span class="error"><?=isset($results['LastName']) ? $results['LastName'] : '' ?></span>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="Keyword_id">User Type:</label>
                        <div class="controls">
                        	<select name="Keyword_id" id="Keyword_id" class="<?=isset($results['Keyword_id']) ? 'error' : '' ?>">
                            	<option value="">User Type</option>
								<? while($krow = $keywords->fetch_assoc()):  ?>
                                	<option
                                    	value="<?=$krow['id']?>"
                                        <? if($krow['id'] == $row['Keyword_id']){ ?>selected='selected'<? } ?>
                                        ><?=$krow['Name']?></option>
                                <? endwhile; ?>
                            </select>
                            <span class="error"><?=isset($results['Keyword_id']) ? $results['Keyword_id'] : '' ?></span>
                        </div>
                    </div>
                    <div class="control-group">
                    	<div class="controls">
                            <input type="hidden" name="id" value="<?=$row['id']?>" />
                    		<input class="btn btn-primary" type="submit" value="Save" />
                            <a href="./" class="cancel-link">Cancel</a>
                        </div>
                    </div>
                </form>
           </div>
       </div>
       <script type="application/javascript" src="http://code.jquery.com/ui/1.9.1/jquery-ui.js">
	   </script>
       <script type="application/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/jquery.validate.min.js">
	   </script>
       <script type="text/javascript">
	   		$(function(){
				//$("#Keyword_id").val('<?=$row['Keyword_id']?>');
				$("input[type='date']").datepicker();
				
				$("form").validate({
					rules: {
						Keyword_id: { required: true }
					}
				});
				
			});
	   </script>
</body>
</html>






